﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlsqlFormat
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();             
        }

        private void TxtIn_TextChanged(object sender, EventArgs e)
        {
            FolderBrowserDialog op = new FolderBrowserDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                TxtIn.Text = op.SelectedPath;
            }
        }

        private void TxtOut_TextChanged(object sender, EventArgs e)
        {
            FolderBrowserDialog op = new FolderBrowserDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                TxtOut.Text = op.SelectedPath;
            }
        }

        private void BtnDO_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(TxtIn.Text) && !string.IsNullOrEmpty(TxtOut.Text))
            {
                var fom = new Format();
                var dir = new DirectoryInfo(TxtIn.Text);
                var code = Encoding.GetEncoding("Shift-JIS");
                foreach (var file in dir.GetFiles("*.sql"))
                {
                    var txt = File.ReadAllText(file.FullName, code);
                    txt = fom.FomartSql(txt);
                    var path = Path.Combine(TxtOut.Text, file.Name);
                    File.WriteAllText(path, txt, code);
                }
                MessageBox.Show("OK");                
            }
        }
       
    }
}
